﻿using System;
using System.Collections.Generic;
using System.Web;
using FancyIdea.PingShui.Core.Dal;
using FancyIdea.PingShui.Core.Util;
using FancyIdea.PingShui.Core.DbHelper;
using System.Data;

namespace FancyIdea.PingShui.Core.Bll
{
    /// <summary>
    /// 行业 的摘要说明
    /// </summary>
    public class 行业: HttpHandler
    {

        #region 行业
        private void Save()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string method = GetStringValue("m");
            string 行业代码 = GetStringValue("行业代码");
            string 行业名称 = GetStringValue("行业名称");
            float 成本利润率 = GetFloatValue("成本利润率");
            if (method.Equals("add"))
            {
                if (行业操作.AddIndustry(行业代码, 行业名称, 成本利润率))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "添加失败", null);
                }
            }
            else if (method.Equals("edit"))
            {
                if (行业操作.UpdateIndustry(行业代码, 行业名称, 成本利润率))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "修改失败", null);
                }
            }
        }

        private void Get()
        {
            string 行业代码 = GetStringValue("行业代码");
            if (string.IsNullOrEmpty(行业代码))
            {
                WriteReturnXml(-1, "系统错误", null);
                return;
            }
            DataRow row = 行业操作.GetIndustry(行业代码);
            if (row != null)
            {
                WriteReturnXml(0, "", row.Table);
            }
            else
            {
                WriteReturnXml(-1, "未查询到数据", null);
            }
        }

        private void List()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            SimpleQueryBuilder SqlBuilder
          = new SimpleQueryBuilder()
          {
              FieldsString = @"a.*",
              FromString = @"from 行业表 a ",
              PageOrderString = "order by a.行业代码",
              FieldsStringForCount = "count(a.行业代码) as count"
          };
            List<string> con = new List<string>();
            con.Add("a.行业代码 like '%" + GetStringValue("行业代码") + "%'");
            con.Add("a.行业名称 like '%" + GetStringValue("行业名称") + "%'");
            SqlBuilder.Conditions = con.ToArray();
            int page = GetInt32Value("page") < 1 ? 1 : GetInt32Value("page");
            string sql = SqlBuilder.BuildQuerySql(page, Const.dataPerPage);
            string sql1 = SqlBuilder.BuildQueryStatisticSql();
            DataTable table = CmdUtil.ExcuteDataTable(sql);
            object o = CmdUtil.ExcuteScalar(sql1);
            int count = int.Parse(o == null ? "0" : o.ToString());
            int total = count % Const.dataPerPage == 0 ?
                count / Const.dataPerPage : (count / Const.dataPerPage) + 1;
            WriteListXml(0, "成功", count, Const.dataPerPage, total, page, table);
        }

        private void DropDown()
        {
            DataTable table = 行业操作.GetIndustryDropDown();
            if (table != null && table.Rows.Count > 0)
            {
                WriteReturnXml(0, null, table);
            }
            else
            {
                WriteReturnXml(-1, "查询失败", table);
            }
        }

        private void Delete()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string 行业代码 = GetStringValue("行业代码");
            if (string.IsNullOrEmpty(行业代码))
            {
                WriteReturnXml(-1, "系统错误", null);
                return;
            }
            if (行业操作.DeleteIndustry(行业代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void DeleteMulti()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string[] 行业代码 = GetStringsValue("行业代码");
            if (行业操作.DeleteIndustry(行业代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }
        #endregion

        #region 行业细目
        private void SaveDetail()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业细目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string method = GetStringValue("m");
            string 行业代码 = GetStringValue("行业代码");
            string 行业细目名称 = GetStringValue("行业细目名称");
            string 行业细目代码 = GetStringValue("行业细目代码");
            if (method.Equals("add"))
            {
                if (行业操作.AddIndustryDetail(行业代码, 行业细目名称, 行业细目代码))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "添加失败", null);
                }
            }
            else if (method.Equals("edit"))
            {
                if (行业操作.UpdateIndustryDetail(行业代码, 行业细目名称, 行业细目代码))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "修改失败", null);
                }
            }
        }
        private void GetDetail()
        {
            string 行业细目代码 = GetStringValue("行业细目代码");
            if (string.IsNullOrEmpty(行业细目代码))
            {
                WriteReturnXml(-1, "系统错误", null);
                return;
            }
            DataRow row = 行业操作.GetIndustryDetail(行业细目代码);
            if (row != null)
            {
                WriteReturnXml(0, "", row.Table);
            }
            else
            {
                WriteReturnXml(-1, "未查询到数据", null);
            }
        }
        private void DeleteDetail()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业细目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string 行业细目代码 = GetStringValue("行业细目代码");
            if (string.IsNullOrEmpty(行业细目代码))
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            if (行业操作.DeleteIndustryDetail(行业细目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void DeleteDetailMulti()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业细目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string[] 行业细目代码 = GetStringsValue("行业细目代码");
            if (行业操作.DeleteIndustryDetail(行业细目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void ListDetail()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业细目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            SimpleQueryBuilder SqlBuilder = new SimpleQueryBuilder()
            {
                FieldsString = @"a.*,b.行业名称 ",
                FromString = @"from 行业细目表 a 
left join 行业表 b on b.行业代码=a.行业代码 ",
                PageOrderString = "order by a.行业细目代码 desc",
                FieldsStringForCount = "count(a.行业细目代码) as count"
            };

            List<string> con = new List<string>();
            con.Add("a.行业细目代码 like '%" + GetStringValue("行业细目代码") + "%'");
            con.Add("a.行业细目名称 like '%" + GetStringValue("行业细目名称") + "%'");
            SqlBuilder.Conditions = con.ToArray();
            int page = GetInt32Value("page") < 1 ? 1 : GetInt32Value("page");
            string sql = SqlBuilder.BuildQuerySql(page, Const.dataPerPage);
            string sql1 = SqlBuilder.BuildQueryStatisticSql();
            DataTable table = CmdUtil.ExcuteDataTable(sql);
            object o = CmdUtil.ExcuteScalar(sql1);
            int count = int.Parse(o == null ? "0" : o.ToString());
            int total = count % Const.dataPerPage == 0 ?
                count / Const.dataPerPage : (count / Const.dataPerPage) + 1;
            WriteListXml(0, "成功", count, Const.dataPerPage, total, page, table);
        }

        private void DropDownDetail()
        {
            string 行业代码 = GetStringValue("行业代码");
            DataTable table = null;
            if (string.IsNullOrEmpty(行业代码))
            {
                table = 行业操作.GetIndustryDetailDropDown();
            }
            else
            {
                table = 行业操作.GetIndustryDetailDropDown(行业代码);
            }
            if (table != null && table.Rows.Count > 0)
            {
                WriteReturnXml(0, null, table);
            }
            else
            {
                WriteReturnXml(-1, "查询失败", table);
            }
        }

        #endregion

        #region 行业细目参数
        private void SaveDetailPara()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
                权限列表.行业细目参数维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            int ID = GetInt32Value("ID");
            string 税务机关代码 = GetStringValue("税务机关代码");
            string 行业代码 = GetStringValue("行业代码");
            string 行业细目代码 = GetStringValue("行业细目代码");
            float 折旧费 = GetFloatValue("单位面积装修折旧费参考");
            float 折旧预警幅度 = GetFloatValue("折旧费预警幅度");
            float 水费 = GetFloatValue("单位面积水费参考");
            float 水费预警幅度 = GetFloatValue("水费预警幅度");
            float 电费 = GetFloatValue("单位面积电费参考");
            float 电费预警幅度 = GetFloatValue("电费预警幅度");
            float 燃气费 = GetFloatValue("单位面积燃气费参考");
            float 燃气费预警幅度 = GetFloatValue("燃气费预警幅度");
            if (ID < 0)
            {
                if (行业操作.AddDetailPara(税务机关代码, 行业代码, 行业细目代码, 折旧费, 折旧预警幅度, 水费, 水费预警幅度,
                    电费, 电费预警幅度, 燃气费, 燃气费预警幅度))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "添加失败", null);
                }
            }
            else
            {
                if (行业操作.UpdateDetailPara(ID, 税务机关代码, 行业代码, 行业细目代码, 折旧费, 折旧预警幅度, 水费, 水费预警幅度,
                    电费, 电费预警幅度, 燃气费, 燃气费预警幅度))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "修改失败", null);
                }
            }
        }

        private void GetDetailPara()
        {
            int ID = GetInt32Value("ID");
            if (ID < 0)
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            DataRow row = 行业操作.GetDetailPara(ID);
            if (row != null)
            {
                WriteReturnXml(0, "", row.Table);
            }
            else
            {
                WriteReturnXml(-1, "未查询到数据", null);
            }
        }

        private void DeleteDetailPara()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.行业细目参数维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string 行业细目代码 = GetStringValue("行业细目代码");
            if (string.IsNullOrEmpty(行业细目代码))
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            if (行业操作.DeleteDetailPara(行业细目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void DeleteDetailParaMulti()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.行业细目参数维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string[] 行业细目代码 = GetStringsValue("行业细目代码");
            if (行业操作.DeleteDetailPara(行业细目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void ListDetailPara()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.行业细目参数维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            SimpleQueryBuilder SqlBuilder
         = new SimpleQueryBuilder()
         {
             FieldsString = @"a.*,b.行业细目名称,c.行业名称,d.税务机关名称",
             FromString = @"from 行业细目参数表 a 
left join 行业细目表 b on b.行业细目代码=a.行业细目代码 
left join 行业表 c on c.行业代码=a.行业代码 
left join 税务机关表 d on d.税务机关代码=a.税务机关代码",
             PageOrderString = "order by a.行业细目代码",
             FieldsStringForCount = "count(a.行业细目代码) as count"
         };
            List<string> con = new List<string>();
            con.Add("b.行业细目名称 like '%" + GetStringValue("行业细目名称") + "%'");
            con.Add("b.行业细目代码 like '%" + GetStringValue("行业细目代码") + "%'");
            con.Add("d.税务机关代码 like '%" + GetStringValue("税务机关代码") + "%'");
            SqlBuilder.Conditions = con.ToArray();
            int page = GetInt32Value("page") < 1 ? 1 : GetInt32Value("page");
            string sql = SqlBuilder.BuildQuerySql(page, Const.dataPerPage);
            string sql1 = SqlBuilder.BuildQueryStatisticSql();
            DataTable table = CmdUtil.ExcuteDataTable(sql);
            object o = CmdUtil.ExcuteScalar(sql1);
            int count = int.Parse(o == null ? "0" : o.ToString());
            int total = count % Const.dataPerPage == 0 ?
                count / Const.dataPerPage : (count / Const.dataPerPage) + 1;
            WriteListXml(0, "成功", count, Const.dataPerPage, total, page, table);
        }
        #endregion

    }
}